package com.company.app.boImpl;

import java.util.List;

import com.company.app.bo.ProductBO;
import com.company.app.dao.ProductDAO;
import com.company.app.dao.ProductTypeDAO;
import com.company.app.entities.Product;

/**
 * The Class ProductBOImpl.
 */
public class ProductBOImpl implements ProductBO {

	/** The product dao. */
	private ProductDAO productDAO;
	
	private ProductTypeDAO productTypeDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.company.app.bo.ProductBO#getProductById(java.lang.Long)
	 */
	@Override
	public Product getProductById(Long id) {
		return this.productDAO.getEntityById(id); 
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.bo.ProductBO#searchProductByFilter(com.company.app.entities
	 * .Product)
	 */
	@Override
	public List<Product> searchProducts(List<Product> product) {
		return this.productDAO.searchEntities(product);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.company.app.bo.ProductBO#executeProductBussinessLogic()
	 */
	@Override
	public boolean executeProductBussinessLogic() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.bo.ProductBO#createProduct(com.company.app.entities.Product
	 * )
	 */
	@Override
	public Product createProduct(Product product) {
		return this.productDAO.createEntity(product);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.bo.ProductBO#updateProduct(com.company.app.entities.Product
	 * )
	 */
	@Override
	public boolean updateProduct(Product product) {
		return this.productDAO.updateEntity(this.buildProduct(product))!=null;
	}
	
	/**
	 * Builds the product.
	 *
	 * @param param the Product
	 * @return the product
	 */
	private Product buildProduct(Product param){
		Product productEntity = this.getProductById(param.getId());
		if (productEntity!=null){
			if (param.getCode()!=null){
				productEntity.setCode(param.getCode());
			}
			if (param.getName()!=null){
				productEntity.setName(param.getName());
			}
			if(param.getPercSale()!=null){
				productEntity.setPercSale(param.getPercSale());
			}
			if(param.getPrice()!=null){
				productEntity.setPrice(param.getPrice());
			}
			if (param.getPriceOffer()!=null){
				productEntity.setPriceOffer(param.getPriceOffer());
			}
			if(param.getQuantity()!=null){
				productEntity.setQuantity(param.getQuantity());
			}
			if(param.getDateSaleFrom()!=null){
				productEntity.setDateSaleFrom(param.getDateSaleFrom());
			}
			if (param.getDateSaleUntil()!=null){
				productEntity.setDateSaleUntil(param.getDateSaleUntil());
			}
			if (param.getDescription()!=null){
				productEntity.setDescription(param.getDescription());
			}
			if (param.getProductType()!=null && param.getProductType().getId()!=null ){
				productEntity.setProductType(this.productTypeDAO.getEntityById(param.getProductType().getId()));
			}
		}
		return productEntity;
	}
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.bo.ProductBO#deleteProduct(com.company.app.entities.Product
	 * )
	 */
	@Override
	public boolean deleteProduct(Product product) {
		this.productDAO.deleteEntity(product);
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.company.app.bo.ProductBO#getAllProduct()
	 */
	@Override
	public List<Product> getAllProduct() {
		return this.productDAO.getAllEntity();
	}

	/**
	 * Sets the product dao.
	 * 
	 * @param productDAO
	 *            the new product dao
	 */
	public void setProductDAO(ProductDAO productDAO) {
		this.productDAO = productDAO;
	}

	/**
	 * Sets the product type dao.
	 *
	 * @param productTypeDAO the new product type dao
	 */
	public void setProductTypeDAO(ProductTypeDAO productTypeDAO) {
		this.productTypeDAO = productTypeDAO;
	}
	
	

	

}

